package utils

import (
	"fmt"
	"image/color"
	"strconv"
	"strings"
)

// ToString 将不同类型的数值转换为字符串
func ToString(value interface{}) string {
	switch v := value.(type) {
	case int:
		return strconv.Itoa(v)
	case uint32:
		return strconv.FormatUint(uint64(v), 10)
	case uint64:
		return strconv.FormatUint(v, 10)
	case float32:
		return strconv.FormatFloat(float64(v), 'f', -1, 32)
	case float64:
		return strconv.FormatFloat(v, 'f', -1, 64)
	default:
		return ""
	}
}
func ParseInt(s string) int {
	num, err := strconv.Atoi(s)
	if err != nil {
		return 0
	}
	return num
}
func ParseInt32(s string) int32 {
	num, err := strconv.Atoi(s)
	if err != nil {
		return 0
	}
	return int32(num)
}
func ParseInt64(s string) int64 {
	num, err := strconv.Atoi(s)
	if err != nil {
		return 0
	}
	return int64(num)
}
func ParseUint32(s string) uint32 {
	num, err := strconv.Atoi(s)
	if err != nil {
		return 0
	}
	return uint32(num)
}
func ParseUint64(s string) uint64 {
	num, err := strconv.Atoi(s)
	if err != nil {
		return 0
	}
	return uint64(num)
}

func ParseFloat32(s string) float32 {
	num, err := strconv.ParseFloat(s, 32)
	if err != nil {
		return 0
	}
	return float32(num)
}

func ParseFloat64(s string) float64 {
	num, err := strconv.ParseFloat(s, 64)
	if err != nil {
		return 0
	}
	return num
}

// ParseColor 字符串转换为 color.RGBA
func ParseColor(value string) (color.Color, error) {
	parts := strings.Fields(value)
	if len(parts) != 3 {
		return nil, fmt.Errorf("invalid color format")
	}

	r, err := strconv.Atoi(parts[0])
	if err != nil {
		return nil, err
	}
	g, err := strconv.Atoi(parts[1])
	if err != nil {
		return nil, err
	}
	b, err := strconv.Atoi(parts[2])
	if err != nil {
		return nil, err
	}
	return color.RGBA{uint8(r), uint8(g), uint8(b), 255}, nil // Alpha 设置为 255
}

// SplitInt 分割字符串,转换成整形数组
func SplitInt(s string, sep string) []int64 {
	arr := strings.Split(s, sep)
	result := make([]int64, 0)
	for _, v := range arr {
		num, err := strconv.Atoi(v)
		if err != nil {
			continue
		}
		result = append(result, int64(num))
	}
	return result
}
